//
//  AboutMyCollectionReusableView.m
//  XiRan-iOS
//
//  Created by 韩强 on 1/2/20.
//  Copyright © 2020 Wood. All rights reserved.
//

#import "AboutMyCollectionReusableView.h"
@interface AboutMyCollectionReusableView ()
/// 图标
@property(nonatomic, strong) UIImageView *markIV;

/// 标题
@property(nonatomic, strong) UILabel *titleLB;

/// 更多
@property(nonatomic, strong) UIButton *moreButton;

/// 关于我们内容展示
@property (nonatomic, strong) UILabel *contentLabel;


/// 图片
@property(nonatomic, strong) UIImageView *iconImageView;;
@end
@implementation AboutMyCollectionReusableView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self contentLabel];
    }
    return self;
}

- (UIImageView *)markIV {
    if (_markIV == nil) {
        _markIV = [[UIImageView alloc] init];
        _markIV.image = [UIImage imageNamed:@"ic_mark"];
        [self addSubview:_markIV];
        
        [_markIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(16);
            make.top.equalTo(self.mas_top).offset(5);
        }];

    }
    return _markIV;
}

- (UILabel *)titleLB {
    if (_titleLB == nil) {
        _titleLB = [[UILabel alloc] init];
        _titleLB.font = MWFont(18);
        _titleLB.textColor = MWHexColor(@"#333333");
        _titleLB.text = [[MWConfigManager manager] objectForKey:@"about_us" defaultValue:@"关于我们"];
        [self addSubview:_titleLB];

        [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.markIV.mas_right).offset(16);
            make.centerY.equalTo(self.markIV);
        }];
    }
    return _titleLB;
}

- (UIButton *)moreButton {
    if (_moreButton == nil) {
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreButton.titleLabel.font = MWFont(12);
        [_moreButton setTitleColor:MWHexColor(@"#666666") forState:UIControlStateNormal];
        [_moreButton setTitleColor:MWHexColor(@"#666666") forState:UIControlStateHighlighted];
        [_moreButton setTitle:[[MWConfigManager manager] objectForKey:@"view_more" defaultValue:@"查看更多"] forState:UIControlStateNormal];
        [_moreButton setTitle:[[MWConfigManager manager] objectForKey:@"view_more" defaultValue:@"查看更多"] forState:UIControlStateHighlighted];
        [_moreButton addTarget:self action:@selector(moreButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_moreButton setIconInRightWithSpacing:10];
        [self addSubview:_moreButton];
        
        [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-16);
            make.centerY.equalTo(self.titleLB);
            
            NSDictionary *btAtt = @{NSFontAttributeName : MWFont(12)};
            CGSize btSize = [[[MWConfigManager manager] objectForKey:@"view_more" defaultValue:@"查看更多"] sizeWithAttributes:btAtt];
            CGFloat with = btSize.width + 10;
            
            make.width.equalTo(@(with));
        }];
    }
    return _moreButton;
}

- (void)moreButtonClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(lookMore)]) {
        [self.delegate lookMore];
    }
}


- (UIImageView *)iconImageView {
    if (_iconImageView == nil) {
        [self moreButton];
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        _iconImageView.backgroundColor = [UIColor whiteColor];
        _iconImageView.layer.cornerRadius = 5;
        _iconImageView.layer.masksToBounds = YES;
        [self addSubview:_iconImageView];
        
        [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.moreButton.mas_bottom).offset(5);
            make.left.equalTo(self.markIV.mas_left);
            make.right.equalTo(self.moreButton.mas_right);
            make.height.mas_lessThanOrEqualTo(@150);
        }];
    }
    return _iconImageView;
}

- (void)setImages:(NSMutableArray *)images {
    _images = images;
    NSString *urlStr;
    if (_images && images.count > 0) {
        urlStr = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,h_450", images[0]];
        
    } else {
        urlStr = @"";
    }
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"lunbo"]];
}

- (UILabel *)contentLabel {
    if (_contentLabel == nil) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont systemFontOfSize:19];
        [self addSubview:_contentLabel];
        
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(16);
            make.right.equalTo(self).offset(-16);
            make.top.equalTo(self.iconImageView.mas_bottom).offset(10);
            make.bottom.equalTo(self).offset(-16);
        }];
    }
    return _contentLabel;
}

- (void)setContentString:(NSString *)contentString {
    _contentString = contentString;
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[contentString dataUsingEncoding:NSUnicodeStringEncoding] options:options documentAttributes:nil error:nil];
    _contentLabel.attributedText = attrStr;
}
@end
